package com.zhongzhi.nn.push.dataSource;

import java.io.InputStream;
import java.util.Properties;

import com.zhongzhi.nn.push.configure.JdbcConstants;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MybatisConstants {
	
	private static final String resource = "mybatis-config.xml";
	
	static SqlSessionFactory sqlSessionFactory;
	
	public static SqlSession getSqlSession() throws Exception{
		if(sqlSessionFactory == null) {
			Properties properties = new Properties();
			properties.putAll(JdbcConstants.getMap());
			InputStream inputStream = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,null,properties);
			return sqlSessionFactory.openSession();
		}else {
			return sqlSessionFactory.openSession();
		}
	}
}
